/**
 * Created by DELL on 2017/5/10.
 */
var houseId=common.getQueryString("houseId");
var $form=$("form");
var validateForm=$form.validate({
    /**
     * ignore：忽略某些元素不验证
     * @type {String}
     * 例如：ignore: ".ignore" ，忽略所有的带ignore class 的标签
     */
    ignore: ".ignore"
});
window.formObject = $form.submit(function() {
    if(validateForm.form()){
        var data = common.formToJson($(this));
        $.post(window.globalHost + common.esheyiHouseUpdatePath, data).success(function(data) {
            layer.alert(data.res.msg);
            if ("0" == data.code) {
                //如果更新成功，重新渲染页面
                renderPage();
            }
        });
    }
    return false;
});
var positionArr=[];
var $villageList=$form.find("#village-list");
var $villageName=$form.find("#villageName");
renderPage();
function renderPage() {
    $.post(window.globalHost+common.esheyiHouseInfoPath,{id:houseId}).success(function (data) {
        var detailInfo=common.transformData(data);
        common.initInputByName(detailInfo);
        var villageId=detailInfo.villageId;
        //由于在回调数据内没有给出小区信息，因此需要根据小区id调用小区详情接口获取数据，渲染小区信息
        $.post(window.globalHost + '/agent-sys/admin/villageInfo/getDetail.do', {id:villageId}).success(function(village) {
            var villageInfo=common.transformData(village);
            //组装数据，用一个数组存放经纬度信息。数组第一项为经度lng，第二项为维度lat
            !!villageInfo.longitude?positionArr.push(Number(villageInfo.longitude)):null;
            !!villageInfo.latitude?positionArr.push(Number(villageInfo.latitude)):null;
            //根据请求返回的数据信息在当前地图上打点
            myMap.getPositionByBaiDuApi({
                cityName:villageInfo['province']+villageInfo['city'],//传入坐标点所属的省，市二级信息，如果不传，默认是上海市
                initPosition:positionArr//当前根据数据包装产生的坐标点数组
            });
            var editVillage='<span id="editVillageBox" style="color:#999;padding-left: 20px;">发现小区信息错误?<a href="javascript:;" id="editVillage">点我编辑小区</a></span>';
            villageInfo.villageAddress?$("#villageAddress").html(villageInfo.villageAddress+editVillage):null;
            villageInfo.buildYear?$("#buildYear").html(villageInfo.buildYear+"年"):null;
            $("#editVillage").off("click").on("click",function (e) {
                common.openInsertDialog({
                    'url': './basicInfo/villageInfo.html?villageId='+villageId,
                    'title': '小区详情',
                    'sAjaxSource': window.globalHost + '/agent-sys/admin/villageInfo/update.do',
                    'area': ['1000px', '600px'],
                    'btn': '保存',
                    ok: function (record) {
                        //如果更新小区成功，则刷新当前房源详情页面
                        window.location.reload();
                    }
                });
            });
            //创建模糊查询小区表功能模块，根据输入框内输入的内容生成筛选列表，点击筛选列表，相关数据自动填入表单内的相关控件
            utils.searchVillageName({
                villageNameInput:$villageName,
                listPanel:$villageList
            });
        });

        common.getRadioChecked("houseOrientations",detailInfo.houseOrientations);
        common.getRadioChecked("decorationCondition",detailInfo.decorationCondition);
        common.getRadioChecked("houseType",detailInfo.houseType);
        common.getRadioChecked("lockType",detailInfo.lockType);
        common.getCheckboxChecked(".checkbox-houseAdvantage", detailInfo.houseAdvantage);
        common.getCheckboxChecked(".checkbox-houseFacilities", detailInfo.houseFacilities);
    });
}
